﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PinLayoutMVC.Models;

namespace PinLayoutMVC.Core
{
    public class DummyData
    {

        private static SQL2005_819929_smrEntities dbEntity = new SQL2005_819929_smrEntities();

        public static SurveyOverviewModel GetData()
        {
            if (HttpContext.Current.Application["surveyoverview"] == null)
                addSurveyToSession();

            return (SurveyOverviewModel)HttpContext.Current.Application["surveyoverview"]; 
        }

        public static AnswerModel GetAnswerById(int id)
        {
            foreach (SurveyModel survey in DummyData.GetData().Surveys)
            {
                foreach (QuestionModel question in survey.Questions)
                {
                   return question.Answers.Where(a => a.ID == id).FirstOrDefault();
                }
            }

            return null;
        }

        private static void addSurveyToSession()
        {
            SurveyOverviewModel surveyOverview = new SurveyOverviewModel();
            surveyOverview.Surveys.Add(GetSurvey1());
            surveyOverview.Surveys.Add(GetSurvey2());

            surveyOverview.lastQuestionId = questionid;
            surveyOverview.lastAnswerId = answerid;

            HttpContext.Current.Application.Add("surveyoverview", surveyOverview);
        }

        public static List<SurveyModel> GetSurveyTemplate(long id)
        {

            var SurveyQuery = from i in dbEntity.Surveys
                         where i.SurveyID == id
                         select new SurveyModel
                         {
                            ID = (int)i.SurveyID,
                            Name = i.Name,
                            Description = i.Description,
                            Published = false,
                            CreationDate = i.StartDate.Value
                         };

            var QuestionQuery = from j in dbEntity.Questions
                    where j.SurveyID == id
                    from k in dbEntity.Answers
                    where k.QuestionID == j.ID
                    select new QuestionModel
                    {
                        SurveyID = (int)j.SurveyID,
                        ID = (int)j.ID,
                        Number = (int)j.Nr,
                        Question = j.Text,
                        Description = j.Description,
                        Type = QuestionModel.GetQuestionType(j.Type),
                        AllowFeedback = (bool) j.AllowCustomAnswer,
                        FeedbackText = ""
                    };

            var AnswerQuery = from j in dbEntity.Questions
                              where j.SurveyID == id
                              from k in dbEntity.Answers
                              where k.QuestionID == j.ID
                              select new AnswerModel
                              {
                                  QuestionID = (int)j.ID,
                                  ID = (int)k.ID,
                                  Text = k.Text,
                                  Category = AnswerModel.GetAnswerCategory(k.Type),
                                  Number = (int)k.Weight
                              };


            List<SurveyModel> listSurvey = SurveyQuery.ToList();
            List<QuestionModel> listQuestion = QuestionQuery.ToList();
            List<AnswerModel> listAnswer = AnswerQuery.ToList();

            foreach (SurveyModel tempSurvey in listSurvey)
            {
                foreach (QuestionModel tempQuestion in listQuestion)
                {
                    foreach (AnswerModel tempAnswer in listAnswer)
                    {
                        tempQuestion.Answers.Add(tempAnswer);           
                    }
                    tempSurvey.Questions.Add(tempQuestion);
                }
            }

            return listSurvey;
        }

        private static SurveyModel GetSurvey1()
        {
            SurveyModel survey = new SurveyModel();

            survey.ID = 1;
            survey.Name = "Vragenlijst overgewicht";
            survey.Description = "Vragen over het overgewicht van mensen.";
            survey.CreationDate = DateTime.Parse("2009-12-31");
            survey.Published = true;

            QuestionModel question1 = new QuestionModel();

            question1.ID = questionId();
            question1.SurveyID = 1;
            question1.Number = 1;
            question1.Question = "Bent u te dik?";
            question1.Description = "Dit is bij een BMI hoger als 25";
            question1.AllowFeedback = true;
            question1.FeedbackText = "Hoe komt dit?";
            question1.Type = QuestionModel.EQuestionType.radiobutton;

            AnswerModel answer1a = new AnswerModel();

            answer1a.ID = answerId();
            answer1a.Number = 1;
            answer1a.Text = "ja";
            answer1a.LeadsToQuestion = 3;

            AnswerModel answer1b = new AnswerModel();

            answer1b.ID = answerId();
            answer1b.Number = 2;
            answer1b.Text = "nee";
            answer1b.LeadsToQuestion = 0;


            AnswerModel answer1c = new AnswerModel();

            answer1c.ID = answerId();
            answer1c.Number = 3;
            answer1c.Text = "Weet niet";
            answer1c.LeadsToQuestion = 2;

            question1.Answers.Add(answer1a);
            question1.Answers.Add(answer1b);
            question1.Answers.Add(answer1c);

            QuestionModel question2 = new QuestionModel();

            question2.ID = questionId();
            question2.Number = 2;
            question2.SurveyID = 1;
            question2.Question = "Weegt u zwaarder als 100kg?";
            question2.Description = "";
            question2.AllowFeedback = false;
            question2.FeedbackText = "";
            question2.Type = QuestionModel.EQuestionType.radiobutton;

            AnswerModel answer2a = new AnswerModel();

            answer2a.ID = answerId();
            answer2a.Number = 1;
            answer2a.Text = "ja";
            answer2a.LeadsToQuestion = 3;

            AnswerModel answer2b = new AnswerModel();

            answer2b.ID = answerId();
            answer2b.Number = 2;
            answer2b.Text = "nee";
            answer2b.LeadsToQuestion = 0;

            question2.Answers.Add(answer2a);
            question2.Answers.Add(answer2b);

            QuestionModel question3 = new QuestionModel();

            question3.ID = questionId();
            question3.SurveyID = 1;
            question3.Number = 3;
            question3.Question = "Hoevaak heeft u geprobeerd af te vallen?";
            question3.Description = "";
            question3.AllowFeedback = true;
            question3.FeedbackText = "Toelichting:";
            question3.Type = QuestionModel.EQuestionType.radiobutton;

            AnswerModel answer3a = new AnswerModel();

            answer3a.ID = answerId();
            answer3a.Number = 1;
            answer3a.Text = "nooit";
            answer3a.LeadsToQuestion = 0;

            AnswerModel answer3b = new AnswerModel();

            answer3b.ID = answerId();
            answer3b.Number = 2;
            answer3b.Text = "eenmalig";
            answer3b.LeadsToQuestion = 4;

            AnswerModel answer3c = new AnswerModel();

            answer3c.ID = answerId();
            answer3c.Number = 3;
            answer3c.Text = "2 of 3 keer";
            answer3c.LeadsToQuestion = 4;

            AnswerModel answer3d = new AnswerModel();

            answer3d.ID = answerId();
            answer3d.Number = 4;
            answer3d.Text = "meer als 3 keer";
            answer3d.LeadsToQuestion = 4;

            question3.Answers.Add(answer3a);
            question3.Answers.Add(answer3b);
            question3.Answers.Add(answer3c);
            question3.Answers.Add(answer3d);

            QuestionModel question4 = new QuestionModel();

            question4.ID = questionId();
            question4.SurveyID = 1;
            question4.Number = 4;
            question4.Question = "Hoe komt het dat deze poging(en) niet zijn gelukt?";
            question4.Description = "";
            question4.AllowFeedback = false;
            question4.FeedbackText = "";
            question4.Type = QuestionModel.EQuestionType.open;

            AnswerModel answer4a = new AnswerModel();

            answer4a.ID = answerId();
            answer4a.Number = 1;
            answer4a.Text = "";
            answer4a.Category = AnswerModel.EAnswerCategory.Open;
            answer4a.LeadsToQuestion = 0;

            question4.Answers.Add(answer4a);

            survey.Questions.Add(question1);
            survey.Questions.Add(question2);
            survey.Questions.Add(question3);
            survey.Questions.Add(question4);

            return survey;
        }

        private static SurveyModel GetSurvey2()
        {
            SurveyModel survey = new SurveyModel();

            survey.ID = 2;
            survey.Name = "Stoppen met roken";
            survey.Description = "Vragen over het rookgedrag";
            survey.CreationDate = DateTime.Parse("2011-02-15");
            survey.Published = false;

            QuestionModel question1 = new QuestionModel();

            question1.ID = questionId();
            question1.SurveyID = 2;
            question1.Number = 1;
            question1.Question = "Rookt u?";
            question1.AllowFeedback = false;
            question1.Type = QuestionModel.EQuestionType.radiobutton;

            AnswerModel answer1a = new AnswerModel();

            answer1a.ID = answerId();
            answer1a.Number = 1;
            answer1a.Text = "ja";
            answer1a.LeadsToQuestion = 3;

            AnswerModel answer1b = new AnswerModel();

            answer1b.ID = answerId();
            answer1b.Number = 2;
            answer1b.Text = "nee";
            answer1b.LeadsToQuestion = 0;

            AnswerModel answer1c = new AnswerModel();

            answer1c.ID = answerId();
            answer1c.Number = 3;
            answer1c.Text = "Niet meer, maar ik heb wel gerookt.";
            answer1c.LeadsToQuestion = 2;

            question1.Answers.Add(answer1a);
            question1.Answers.Add(answer1b);
            question1.Answers.Add(answer1c);

            QuestionModel question2 = new QuestionModel();

            question2.ID = questionId();
            question2.SurveyID = 2;
            question2.Number = 2;
            question2.Question = "Waarom bent u gestopt en heeft u hier hulp bij ontvangen?";
            question2.Description = "";
            question2.AllowFeedback = false;
            question2.FeedbackText = "";
            question2.Type = QuestionModel.EQuestionType.open;

            QuestionModel question3 = new QuestionModel();

            question3.ID = questionId();
            question3.Number = 3;
            question3.SurveyID = 2;
            question3.Question = "Hoelang rookt u al?";
            question3.Description = "";
            question3.AllowFeedback = true;
            question3.FeedbackText = "Toelichting:";
            question3.Type = QuestionModel.EQuestionType.radiobutton;

            AnswerModel answer3a = new AnswerModel();

            answer3a.ID = answerId();
            answer3a.Number = 1;
            answer3a.Text = "minder als 1 jaar";
            answer3a.LeadsToQuestion = 0;

            AnswerModel answer3b = new AnswerModel();

            answer3b.ID = answerId();
            answer3b.Number = 2;
            answer3b.Text = "1 tot 5 jaar";
            answer3b.LeadsToQuestion = 0;

            AnswerModel answer3c = new AnswerModel();

            answer3c.ID = answerId();
            answer3c.Number = 3;
            answer3c.Text = "Meer als 5 jaar";
            answer3c.LeadsToQuestion = 0;

            question3.Answers.Add(answer3a);
            question3.Answers.Add(answer3b);
            question3.Answers.Add(answer3c);

 
            survey.Questions.Add(question1);
            survey.Questions.Add(question2);
            survey.Questions.Add(question3);

            return survey;
        }

        private static int answerid = 0;

        private static int answerId()
        {
            answerid++;
            return answerid;
        }

        private static int questionid = 0;

        private static int questionId()
        {
            questionid++;
            return questionid;
        }

        private static SurveyModel GetSurvey3()
        {
            return null;
        }

        public int InsertSurvey(SurveyModel survey)
        {
            

            Survey oSurvey = new Survey
            {
                Name = survey.Name,
                Description = survey.Description,
                EndDate = DateTime.Now,
                StartDate = survey.CreationDate,
                Status = 1,
                SurveyID = survey.ID,
                Type = 0,
            };

            dbEntity.AddToSurveys(oSurvey);
            dbEntity.SaveChanges();

            return 0;
        }

        public int EditSurvey(long id)
        {
            try
            {

                var editSurvey = from i in dbEntity.Surveys
                                 where i.SurveyID == id
                                 select i;

                foreach (Survey item in editSurvey)
                {
                    // Edit hier de gegeven codes
                }

                return 1;
            }
            catch (Exception)
            {
                return 0;
            }



        }


    }
}